package com.hirsi.common.core.controller;

import com.hirsi.common.core.enums.ExceptionStatusEnum;
import com.hirsi.common.core.exception.ServerRuntimeException;
import com.hirsi.common.core.vo.result.ResponseResult;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
public class ServerErrorController implements ErrorController {
    @Override
    public String getErrorPath() {
        return "/error";
    }

    @RequestMapping("error")
    public ResponseResult<Void> error(HttpServletRequest request, HttpServletResponse response) {
        if (response.getStatus() == 404) throw new ServerRuntimeException(ExceptionStatusEnum.NOT_FOUND);
        throw new ServerRuntimeException();
    }
}
